"use strict";
/*
 * @Description: 帮助模型获取正确日期的工具对象
 * @Author: 莫若省
 * @date: 2025/9/2
 *
 * 功能说明：
 * 1. 支持解析自然语言中的相对日期描述
 * 2. 支持中文数字和阿拉伯数字
 * 3. 支持未来/过去n天/月/年格式
 * 4. 支持"这周五"、"下周五"、"这星期X"、"下星期X"等表达
 * 5. 支持"二十日后"等中文数字+时间单位格式
 * 6. 返回对应的具体日期和时间
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.date_tool = void 0;
var zod_1 = require("zod");
// 日期关键词映射
var DATE_KEYWORDS = {
    '今天': 0, '当天': 0, '现在': 0, '当前': 0,
    '昨天': -1, '前天': -2, '大前天': -3,
    '明天': 1, '后天': 2, '大后天': 3,
    '前年': -730, '去年': -365, '今年': 0, '明年': 365, '后年': 730
};
// 中文数字映射（扩展支持更多中文数字）
var CHINESE_NUMBERS = {
    '零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5,
    '六': 6, '七': 7, '八': 8, '九': 9, '十': 10, '十一': 11,
    '十二': 12, '十三': 13, '十四': 14, '十五': 15, '十六': 16,
    '十七': 17, '十八': 18, '十九': 19, '二十': 20, '三十': 30,
    '四十': 40, '五十': 50, '六十': 60, '七十': 70, '八十': 80,
    '九十': 90, '两': 2, '百': 100, '千': 1000, '万': 10000,
    '廿': 20, '卅': 30, '卌': 40, '皕': 200
};
// 星期映射
var WEEKDAY_MAP = {
    '日': 0, '天': 0, '一': 1, '二': 2, '三': 3,
    '四': 4, '五': 5, '六': 6
};
var dateToolGenerator = function () { return ({
    name: "日期计算器",
    description: "精准解析中文自然语言日期描述，支持相对时间、绝对时间、星期表达、中文数字日期等多种格式，返回具体的日期时间信息",
    prams: {
        text: zod_1.z.string().describe("请输入自然语言日期描述，支持：1.相对时间(3天后/一个月前/二十日后) 2.绝对时间(下周一/这周五) 3.关键词(今天/明天/大后天) 4.时间段(未来7天/过去30天) 5.中文数字(三天后/二十日后/一百天后) "),
    },
    cb: function (_a, extra_1) { return __awaiter(void 0, [_a, extra_1], void 0, function (_b, extra) {
        var now, parseNumber, handleTimeUnit, timeMatch, amount, unit, isFuture, relativeMatch, amount, unit, weekdayMatch, targetDay, currentDay, daysToAdd, thisNextWeekMatch, targetDay, currentDay, daysToAdd, thisNextWeekShortMatch, targetDay, currentDay, daysToAdd;
        var text = _b.text;
        return __generator(this, function (_c) {
            try {
                console.error(extra);
                now = new Date();
                // 处理已知关键词
                if (DATE_KEYWORDS[text] !== undefined) {
                    now.setDate(now.getDate() + DATE_KEYWORDS[text]);
                    return [2 /*return*/, formatDateResponse(now)];
                }
                parseNumber = function (str) {
                    // 先尝试直接解析阿拉伯数字
                    if (/^\d+$/.test(str)) {
                        return parseInt(str, 10);
                    }
                    // 解析中文数字
                    var total = 0;
                    var current = 0;
                    for (var _i = 0, str_1 = str; _i < str_1.length; _i++) {
                        var char = str_1[_i];
                        var num = CHINESE_NUMBERS[char];
                        if (num === undefined)
                            continue;
                        if (num >= 10) {
                            if (current === 0)
                                current = 1;
                            total += current * num;
                            current = 0;
                        }
                        else {
                            current += num;
                        }
                    }
                    return total + current;
                };
                handleTimeUnit = function (date, amount, unit, isFuture) {
                    var multiplier = isFuture ? 1 : -1;
                    switch (unit) {
                        case '天':
                            date.setDate(date.getDate() + amount * multiplier);
                            break;
                        case '月':
                            date.setMonth(date.getMonth() + amount * multiplier);
                            break;
                        case '年':
                            date.setFullYear(date.getFullYear() + amount * multiplier);
                            break;
                        case '周':
                            date.setDate(date.getDate() + amount * 7 * multiplier);
                            break;
                    }
                };
                timeMatch = text.match(/^(未来|过去|接下来|之前)([一二三四五六七八九十零两廿卅卌皕百千万\d]+)(天|个?月|年|周|个?星期)/);
                if (timeMatch) {
                    amount = parseNumber(timeMatch[2]);
                    unit = timeMatch[3].replace('个', '').replace('星期', '周');
                    isFuture = ['未来', '接下来'].includes(timeMatch[1]) ||
                        (timeMatch[1] !== '之前');
                    handleTimeUnit(now, amount, unit, isFuture);
                    return [2 /*return*/, formatDateResponse(now)];
                }
                relativeMatch = text.match(/^([一二三四五六七八九十零两廿卅卌皕百千万\d]+)(天|个?月|年|周|个?星期)(前|后|之后)/);
                if (relativeMatch) {
                    amount = parseNumber(relativeMatch[1]);
                    unit = relativeMatch[2].replace('个', '').replace('星期', '周');
                    handleTimeUnit(now, amount, unit, relativeMatch[3] !== '前');
                    return [2 /*return*/, formatDateResponse(now)];
                }
                weekdayMatch = text.match(/^星期([一二三四五六日天])/);
                if (weekdayMatch) {
                    targetDay = WEEKDAY_MAP[weekdayMatch[1]];
                    currentDay = now.getDay();
                    daysToAdd = (targetDay - currentDay + 7) % 7 || 7;
                    now.setDate(now.getDate() + daysToAdd);
                    return [2 /*return*/, formatDateResponse(now)];
                }
                thisNextWeekMatch = text.match(/^(这|下|本周|下周)(周|星期)([一二三四五六日天])/);
                if (thisNextWeekMatch) {
                    targetDay = WEEKDAY_MAP[thisNextWeekMatch[3]];
                    currentDay = now.getDay();
                    daysToAdd = (targetDay - currentDay + 7) % 7;
                    if (thisNextWeekMatch[1] === '下' || thisNextWeekMatch[1] === '下周') {
                        daysToAdd += 7;
                    }
                    if (daysToAdd === 0 && (thisNextWeekMatch[1] === '下' || thisNextWeekMatch[1] === '下周')) {
                        daysToAdd = 7;
                    }
                    now.setDate(now.getDate() + daysToAdd);
                    return [2 /*return*/, formatDateResponse(now)];
                }
                thisNextWeekShortMatch = text.match(/^(这|下)周([一二三四五六日天])/);
                if (thisNextWeekShortMatch) {
                    targetDay = WEEKDAY_MAP[thisNextWeekShortMatch[2]];
                    currentDay = now.getDay();
                    daysToAdd = (targetDay - currentDay + 7) % 7;
                    if (thisNextWeekShortMatch[1] === '下') {
                        daysToAdd += 7;
                    }
                    if (daysToAdd === 0 && thisNextWeekShortMatch[1] === '下') {
                        daysToAdd = 7;
                    }
                    now.setDate(now.getDate() + daysToAdd);
                    return [2 /*return*/, formatDateResponse(now)];
                }
                // 都没有命中,返回未命中提示,让模型重新总结
                return [2 /*return*/, {
                        content: [{
                                text: "\u65E0\u6CD5\u89E3\u6790\u65E5\u671F\u63CF\u8FF0\uFF0C\u8BF7\u91CD\u65B0\u63D0\u53D6\u5173\u952E\u8BCD \u5982 \u4ECA\u5929 \u660E\u5929 \u540E\u5929 \u4E09\u5929\u540E \u4E00\u4E2A\u6708\u540E \u672A\u6765N\u5929 \u8FC7\u53BBN\u5929 \u661F\u671FX \u8FD9\u5468\u4E94 \u4E0B\u5468\u4E00 \u4E8C\u5341\u65E5\u540E \u5F53\u524D\u65F6\u95F4\u4E3A:".concat(now.toLocaleString('zh-CN')),
                                type: "text"
                            }]
                    }];
            }
            catch (error) {
                console.error("日期处理错误:", error);
                return [2 /*return*/, {
                        content: [{
                                text: "无法解析日期描述，已返回当前日期和时间",
                                type: "text"
                            }, {
                                text: new Date().toLocaleString(),
                                type: "text"
                            }]
                    }];
            }
            return [2 /*return*/];
        });
    }); }
}); };
exports.date_tool = dateToolGenerator;
// 格式化日期响应
function formatDateResponse(date) {
    return {
        content: [
            {
                text: "\u60A8\u67E5\u8BE2\u7684\u65E5\u671F\u662F: ".concat(date.toLocaleDateString('zh-CN', {
                    year: 'numeric', month: 'long', day: 'numeric', weekday: 'long'
                })),
                type: "text"
            }
        ]
    };
}
